﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Ionic.Zip;
using MahApps.Metro.Controls;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// CompressDialog.xaml 的交互逻辑
    /// </summary>
    public partial class CompressDialog : MetroWindow
    {
        public CompressDialog()
        {
            InitializeComponent();
            BuildZipPath();
        }

        private string BuildZipPath()
        {
            try
            {
                var workspaceDirectoryInfo = new DirectoryInfo(Globals.PathOfWorkspace);
                if (workspaceDirectoryInfo != null)
                {
                    var parentDirectoryInfo = workspaceDirectoryInfo.Parent;
                    if (parentDirectoryInfo != null)
                    {
                        var zipFilePath = "";
                        zipFilePath = parentDirectoryInfo.FullName;
                        if (zipFilePath.EndsWith("\\") == false)
                            zipFilePath += "\\";
                        var now = DateTime.Now;

                        var month = now.Month.ToString();
                        if (now.Month < 10)
                            month = "0" + month;

                        var day = now.Day.ToString();
                        if (now.Day < 10)
                            day = "0" + day;

                        var h = now.Hour.ToString();
                        if (now.Hour < 10)
                            h = "0" + h;

                        var m = now.Minute.ToString();
                        if (now.Minute < 10)
                            m = "0" + m;

                        var s = now.Second.ToString();
                        if (now.Second < 10)
                            s = "0" + s;

                        zipFilePath += workspaceDirectoryInfo.Name + " " + $"{now.Year}年{month}月{day}日" + " " +
                            $"{h}时{m}分{s}秒" + ".zip";

                        tbZipFilePath.Text = zipFilePath;
                        return zipFilePath;
                    }
                }

                return "";
            }
            catch (Exception ex)
            {
                tbZipFilePath.Text = ex.Message;
                tbZipFilePath.Foreground = Brushes.Red;
                btnZip.IsEnabled = false;
                return "";
            }
        }

        private void BtnZip_Click(object sender, RoutedEventArgs e)
        {
            var zipPath = BuildZipPath();
            try
            {
                var zipFileInfo = new FileInfo(zipPath);

                if (pbx1.Password != pbx2.Password)
                {
                    LMessageBox.Show("两次输入的密码不一致！", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                using (ZipFile zipFile = new ZipFile())
                {
                    zipFile.AlternateEncoding = Encoding.GetEncoding("GBK");
                    zipFile.AlternateEncodingUsage = ZipOption.Always;
                    zipFile.AddDirectory(Globals.PathOfWorkspace);
                    zipFile.Comment = tbxComment.Text;
                    zipFile.Save(zipFileInfo.FullName);
                }

                var answer = LMessageBox.Show("完成文件压缩。要在资源管理器中显示压缩包文件吗？", Globals.AppName, MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (answer == MessageBoxResult.Yes)
                    Process.Start("explorer.exe", "/select, " + zipFileInfo.FullName);//打开资源管理器并选中文件
            }
            catch (Exception ex)
            {
                LMessageBox.Show(ex.Message);
            }
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
